import { UserSettingPage } from './user-setting/setting';
import { UserWodekefuPage } from './user-wodekefu/user-wodekefu';
import { UserYueliaoPage } from './user-yueliao/user-yueliao';
// import { UserXufeihuiyuanPage } from './user-xufeihuiyuan/user-xufeihuiyuan';
import { UserRqPage } from './user-rq/user-rq';
import { UserMywalletPage } from './user-mywallet/user-mywallet';
import { MyChallengePage } from './../home/my-challenge/my-challenge';
import { AllTargetPage } from './../all-target/all-target';
import { Api } from './../../providers/api-service';
import { UrlService } from './../../providers/url-service';
import { Component } from '@angular/core';
import { NavController, ModalController } from 'ionic-angular';
import { UserMycollectPage } from './user-mycollect/user-mycollect';
import { UserLichanPage } from './user-lichan/user-lichan';
import { NoticeService } from '../../providers/notice-service';
import { PersonaldataPage } from './personaldata/personaldata';
// import { UserSannerPage } from './user-sanner/user-sanner';
import { UserDetialChallengePage } from './user-detial-challenge/user-detial-challenge';
import { QRScannerStatus, QRScanner } from '@ionic-native/qr-scanner';

/**
 * 用户信息和设置
 */
@Component({
	selector: 'page-user',
	templateUrl: 'user.html'
})
export class UserPage {
	userData = {
		isVip: false,
		HeadPortrait: null,
		NickName: '未设置',
		Fans: 0,
		GZC: 0,
		Integral: 0,
		DTC: 0,
		PerCentage: ''
	};
	constructor(
		public navCtrl: NavController,
		public modalCtrl: ModalController,
		public api: Api,
		public urlService: UrlService,
		private qrScanner: QRScanner,
		public noticeServe: NoticeService,
	) {
		this.doinite();
	}
	/**
 * 初始化函数
 */
	doinite() {
		// //console.log('ionViewDidLoad LoginPage');
		this.getUuser();
	}

	/**
   * 下拉刷新
   * @param even 
   */
	doRefresh(even) {
		//console.log('下拉刷新');
		//console.log(even);
		setTimeout(() => {
			this.doinite();
			even.complete();
		}, 2000);
	}

	/**
 * 获取用户信息
 */
	getUuser() {
		let id = this.urlService.getUserinfo("UserID");
		this.urlService.getUser(id).then((data) => {
			//console.log('个人信息主页');
			//console.log(data);
			if (data.Code == 0) {
				let user = data.Data;
				//是否是会员
				if (user.IsVip == 1) {
					this.userData.isVip = true;
				}
				if (user.HeadPortrait != null) {

					this.userData.HeadPortrait = user.HeadPortrait;
				}
				if (user.NickName) {
					this.userData.NickName = user.NickName;
				}
				if (user.FSC) {
					this.userData.Fans = user.FSC;
				}
				if (user.Integral) {
					this.userData.Integral = user.Integral;
				}
				if (user.GZC) {
					this.userData.GZC = user.GZC;
				}
				if (user.DTC) {
					this.userData.DTC = user.DTC;
				}
				if (user.PerCentage == '--') {
					this.userData.PerCentage = user.PerCentage;
				} else {
					this.userData.PerCentage = Number(this.userData.PerCentage).toFixed(2);
				}
			}
		});
	}
	//扫一扫
	SaoyiSao() {
		// this.navCtrl.push(UserSannerPage,null,{"animate":false});
		this.BcodeScanner();
	}

	/**
		 * 条形码扫描器插件打开相机视图并自动扫描条形码，并将数据返回给您。
		 */
	BcodeScanner() {
		this.qrScanner.prepare()
			.then((status: QRScannerStatus) => {
				if (status.authorized) {
					let scanSub = this.qrScanner.scan().subscribe((text: string) => {
						alert(text);

						//this.qrScanner.hide(); // hide camera preview
						scanSub.unsubscribe(); // stop scanning
					});
					this.qrScanner.show();

				} else if (status.denied) {
					//提醒用户没有开权限
				} else {

				}
			})
			.catch((e: any) => {
				this.noticeServe.showNoticeByToast(1, '扫描失败');
			});




	}
	getInfo(result) {
		this.urlService.GetbITMPUserInfo(result).then((data) => {
			if (data.Code == 0) {
				this.noticeServe.showNoticeByToast(1, '扫描成功');
				localStorage.setItem('personId', data.code);
				this.navCtrl.push(UserDetialChallengePage, { "userID": data.ID });
			} else {
				this.noticeServe.showNoticeByToast(2, '扫描失败');
				alert(JSON.stringify(data.Mag));
			}
		});
	}

	//设置
	Setting() {
		this.navCtrl.push(UserSettingPage);
	}

	gopage(key) {
		let pageName;
		switch (key) {
			case 'wodemubiao': //我的目标
				pageName = AllTargetPage;
				break;
			case 'wodetiaozhan': //我的挑战
				pageName = MyChallengePage;
				break;
			case 'wodeshoucang': //我的收藏
				pageName = UserMycollectPage;
				break;
			case 'wodeqianbao': //我的钱包
				pageName = UserMywalletPage;
				break;
			case 'wodelicheng': // 我的历程
				pageName = UserLichanPage;
				break;
			case 'wodeerweima': //我的二维码
				pageName = UserRqPage;
				break;
			case 'huiyuanzhuangtai': //会员状态
				pageName = "UserXufeihuiyuanPage";
				break;
			case 'wodeyueliao': // 我的约聊
				pageName = UserYueliaoPage;
				break;
			case 'wodekefu': //我的客服
				pageName = UserWodekefuPage;
				break;
		}
		this.navCtrl.push(pageName);
	}

	/**
	 * 点击头像
	 */
	headerBtn() {
		this.navCtrl.push(PersonaldataPage);
	}
}
